using System;
using System.Reflection;
using System.Runtime.Remoting;

namespace OR.Reuse.Reflection
{

	/// <summary>
	/// The Assembly Utility class to help with dynamic instantiation
	/// </summary>
	public class AssemblyUtility
	{

      /// <summary>
      /// Constructor
      /// </summary>
		private AssemblyUtility()
		{
		}

      /// <summary>
      /// Constructs an instance of a type dynamically.
      /// </summary>
      /// <param name="pAssembly">The name of the assembly containing the type definition.</param>
      /// <param name="pClassName">The fully qualified type name to instantiate.</param>
      /// <param name="pArgs">An array of arguments to be passed to the constructor.</param>
      /// <returns>An instantiated object of the given type.</returns>
		public static object GetInstance( string pAssembly, string pClassName, object[ ] pArgs )
		{
			object result = null;
			string className = String.Empty;
			className = PropertyHandler.CheckString( pClassName );

			ObjectHandle	pHandle	= Activator.CreateInstance( pAssembly, className, true, BindingFlags.Instance | BindingFlags.Public, null, pArgs, null, null, null );

			result = pHandle.Unwrap();

			return result;

		}

		public static object GetInstance( string pClassName, object[ ] pArgs, string[] pTemplateTypes )
		{
			object result = null;

			Type vResult = TypeUtility.GetGenericType( pClassName, pTemplateTypes );

			result = Activator.CreateInstance( vResult );

			return result;
		}

      /// <summary>
      /// Constructs an instance of a type dynamically.
      /// </summary>
      /// <param name="pAssembly">The name of the assembly containing the type definition.</param>
      /// <param name="pClassName">The fully qualified type name to instantiate.</param>
      /// <returns>An instantiated object of the given type.</returns>
		public static object GetInstance( string pAssembly, string pClassName )
		{
			return ( AssemblyUtility.GetInstance( pAssembly, pClassName, new object[0] ) );
		}


	}
}
